#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <strings.h>

/*
  Ecoute la poingner de main pour la connexion entre un serveur et un client
 */

int main(void)
{
  int socket_client,socket_server;
  struct sockaddr_in middle_client_addr,client_addr;
  struct sockaddr_in middle_server_addr,server_addr;
  socklen_t len;
  char message[1024];
  int nb,t;

  /*
    Socket de connection cote client
  */
  socket_client=socket(AF_INET,SOCK_DGRAM,0);
  bzero(&middle_client_addr,sizeof(middle_client_addr));
  middle_client_addr.sin_family=AF_INET;
  /*
    Utiliser \connect addresse:port pour voir lechange
    Addresse de connexion
  */
  middle_client_addr.sin_addr.s_addr=inet_addr(/*"172.17.131.204"*/"127.0.0.1");
  /* 
     Port de connexion
  */
  middle_client_addr.sin_port=htons(44244);
  /*
    Binder la socket
  */
  bind(socket_client,(struct sockaddr*)&middle_client_addr,sizeof(middle_client_addr));

  /*
    Socket de connection cote serveur
  */
  socket_server=socket(AF_INET,SOCK_DGRAM,0);
  bzero(&middle_server_addr,sizeof(middle_server_addr));
  middle_server_addr.sin_family=AF_INET;
  /* 
     Addresse de connexion
  */
  middle_server_addr.sin_addr.s_addr=inet_addr(/*"172.17.131.204"*/"127.0.0.1");
  /*
    Port de connexion du serveur
  */
  middle_server_addr.sin_port=htons(34444);
  bind(socket_server,(struct sockaddr*)&middle_server_addr,sizeof(middle_server_addr));

  for( ; ; )
    {
      /*
	Reception des donnees du client
      */
      len=sizeof(client_addr);
      /*
	recvfrom(socketclient,message,taille,flag,sockaddr libre pour info client,size)
      */
      nb=recvfrom(socket_client,message,1000,0,(struct sockaddr*)&client_addr,&len);

      /*
      printf("-------------------------------------------------------\n");
      message[nb] = 0;
      printf("Received the following CLIENT:\n");
      printf("%s\n",message);
      printf("-------------------------------------------------------\n");
      */

      /*
	sendto(socketserver,message,taille,flag,sockaddr du destinataire,size)
      */
      t=sendto(socket_server,message,nb,0,(struct sockaddr*)&middle_server_addr,sizeof(middle_server_addr));

      /*
      printf("-------------------------------------------------------\n");
      message[nb] = 0;
      printf("Send the following SERVER:\n");
      printf("Send : %i\n%s\n",t,message);
      printf("-------------------------------------------------------\n");
      */

      len=sizeof(server_addr);
      nb=recvfrom(socket_server,message,1000,0,(struct sockaddr*)&server_addr,&len);

      /*
      printf("-------------------------------------------------------\n");
      message[nb] = 0;
      printf("Received the following SERVER:\n");
      printf("%s\n",message);
      printf("-------------------------------------------------------\n");
      */

      t=sendto(socket_client,message,nb,0,(struct sockaddr*)&client_addr,sizeof(client_addr));

      /*
      printf("-------------------------------------------------------\n");
      message[nb] = 0;
      printf("Send the following CLIENT:\n");
      printf("Send : %i\n%s\n",t,message);
      printf("-------------------------------------------------------\n");
      */
    }

  return 0;
}
